using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Net.Mail;
using System.Net;

namespace Axe.Logging
{
    public sealed class MailLogger : ILogger
    {
        SmtpClient m_SMTPClient;

        string m_Recepient;
        string m_From;

        string m_UserName;
        string m_UserPass;
        #region ILogger Members

        public bool Initialize(NameValueCollection props)
        {
            string host = props["host"];
            string port = props["port"];

            m_Recepient = props["targetMail"];
            m_From = props["fromMail"];

            m_UserName = props["userName"];
            m_UserPass = props["userPass"];

            m_SMTPClient = new SmtpClient();

            if (m_UserName == null)
            {
                m_SMTPClient.UseDefaultCredentials = true;    
            }
            else
            {
                m_SMTPClient.Credentials = new NetworkCredential(m_UserName, m_UserPass);
            }

            return true;
        }

        public void Add(LogEntry entry)
        {
            try
            {
                MailMessage mesage = new MailMessage();
                
                mesage.To.Add(m_Recepient);
                mesage.From = new MailAddress(m_From);
                mesage.Subject = String.Format("Nebula Event: {0} {1}", entry.EventType, entry.EventTime);
                mesage.Body = entry.Text;

                m_SMTPClient.Send(mesage);
            }
            finally
            {

            }
        }

        #endregion
    }
}
